package be.phl.layoutAdapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Typeface;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.TextView;
import be.phl.obstacleapp.R;

public class ScreeningItemRowAdapter extends
		ArrayAdapter<ScreeningItemRowAdapter.ScreeningItemRow> {
	
	private final Context context;
	private final List<ScreeningItemRow> screeningItems;
	private static final String TAG = "ScreeningItemRowAdapter";
	public Callbacks mButtonCallback;

	public static final int NEW_PHOTO = 1;
	public static final int GALLERY = 2;
	private int width, height;
	
	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height; 
	}

	/**
	 * A callback interface that returns the button clickevent
	 */
	public interface Callbacks {
		/**
		 * Callback for when an item has been selected.
		 */
		public void onButtonClicked(int itemId, int action);

	}

	public ScreeningItemRowAdapter(Context context,
			List<ScreeningItemRow> screeningItems) {
		super(context, R.layout.screening_item_row, screeningItems);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.screeningItems = screeningItems;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		getScreenDimensions();
		int textsize = getWidth() * 8 / 400;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.screening_item_row, parent,
				false);

		TextView name = (TextView) rowView.findViewById(R.id.name);
		Button buttonPhoto = (Button) rowView.findViewById(R.id.btnPhoto);

		final ScreeningItemRow screeningItem = screeningItems.get(position);
		Typeface tf = Typeface.createFromAsset(context.getAssets(), "com.ttf");
		
		name.setTypeface(tf);
		name.setText(screeningItem.getName());
		name.setTextSize(TypedValue.COMPLEX_UNIT_PX, textsize);

		// check if the item is selected to add a photo button
		PackageManager packageManager = context.getPackageManager();
		 
		// if device support camera?
		if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
			//yes
			Log.i("camera", "This device has camera!");
			buttonPhoto.setVisibility(screeningItem.isSelected() ? View.VISIBLE
					: View.GONE);
		}else{
			//no
			Log.i("camera", "This device has no camera!");
		}
		if (screeningItem.isSelected()) {
			rowView.setBackgroundColor(Color.rgb(130, 150, 225));
		}

		buttonPhoto.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				

				PopupMenu popup = new PopupMenu(context, v);
				MenuInflater inflater = popup.getMenuInflater();
				inflater.inflate(R.menu.button_photo, popup.getMenu());
				popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

					@Override
					public boolean onMenuItemClick(MenuItem item) {

						switch (item.getItemId()) {
						case R.id.new_photo:
							Log.i(TAG, "new photo");
							mButtonCallback.onButtonClicked(
									screeningItem.getId(), NEW_PHOTO);
							return true;
						case R.id.gallery:
							Log.i(TAG, "summary");
							mButtonCallback.onButtonClicked(
									screeningItem.getId(), GALLERY);
							return true;
						default:
							return false;
						}
					}

				});
				popup.show();

			}
		});

		return rowView;
	}

	/**
	 * A screeningroom item representing screeningroom
	 */
	public static class ScreeningItemRow {
		public int id;
		public String name;
		public boolean selected;

		public ScreeningItemRow(int id, String name) {
			this.id = id;
			this.name = name;
		}

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public boolean isSelected() {
			return selected;
		}

		public void setSelected(boolean selected) {
			this.selected = selected;
		}

	}
	
	@SuppressLint("NewApi")
	public void getScreenDimensions() {
//
		if (android.os.Build.VERSION.SDK_INT >= 13) {

			Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
			Point size = new Point();
			display.getSize(size);
			width = size.x;
			height = size.y;
			Log.w(TAG,"width height "+width+" "+height);
		} else {
			Display display = ((Activity)context).getWindowManager().getDefaultDisplay();

			width = display.getWidth(); // deprecated
			height = display.getHeight(); // deprecated
		}

	}

}
